package hzncc.wcs.task;

import hzncc.wcs.modbus.service.ModbusService;
import hzncc.wcs.mqtt.MqttGateway;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.stream.IntStream;

@Component
public class ScheduledTaska {
    @Resource
    private ModbusService modbusService;
    @Resource
    private MqttGateway mqttGateway;

    @Scheduled(cron = "*/30 * * * * ?")
    public void query(){
        //寄存器读取开始地址
        int offset = 0;
        //读取寄存器数量
        int quantity = 10;

        int[] registers = modbusService.readHoldingRegisters(1, offset, 10);
        IntStream.range(0, registers.length).forEach(i -> System.out.println("Index: " + i + ", Value: " + registers[i]));

        if (registers[1] == 1){
            mqttGateway.sendToMqtt("已到达料仓");
            System.out.println("mqtt消息已发送");
        }
//        for (int register : registers) {
//            System.out.println("读取寄存器数据:" + offset++ + "-" + register);
//            //修改寄存器的值
////          modbusMaster.writeSingleRegister(SLAVE_ID, 1, Integer.parseInt(3,));
//        }
        System.out.println("--------------------------------------------");
    }
}
